package com.tro.mail.web;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.tro.mail.dao.entity.MailPabTable;
import com.tro.mail.dao.entity.MailPabTableExample;
import com.tro.mail.dao.mapper.MailPabTableMapper;
import com.tro.mail.util.EasyuiDatagridMode;
import com.tro.mail.util.Json;

@Controller
@RequestMapping("/personalMessage")
public class personalMessage {
	@Autowired
	MailPabTableMapper mailpabmanager;
	
	@ResponseBody
	@RequestMapping("initpersonal")
	public String initPersonal(String id,HttpSession session){
		List<EasyuiDatagridMode> lists=new ArrayList<EasyuiDatagridMode>();
		EasyuiDatagridMode edm=new EasyuiDatagridMode();
		if(StringUtils.isBlank(id)){
			edm.setId("001");
			edm.setGroupName("自定义通讯分组");
			edm.setIconCls("icon-hamburg-category");
			//查询第一层级
			MailPabTableExample me=new MailPabTableExample();
			 me.createCriteria().andGroupCodeEqualTo("001")
			 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
			 List<MailPabTable> mt=mailpabmanager.selectByExample(me);
			List<EasyuiDatagridMode> listg=new ArrayList<EasyuiDatagridMode>();
			for(MailPabTable i:mt){
				EasyuiDatagridMode edm1=new EasyuiDatagridMode();
				edm1.setId(i.getId().toString());
				edm1.setGroupName(i.getGroupName());
				edm1.setGroupCode(i.getGroupCode());
				edm1.setPerson(i.getPerson());
				edm1.setEmail(i.getEmail());
				edm1.setPhone(i.getPhone());
				if("1".equals(i.getPerson())){//为人员
					edm1.setIconCls("icon-hamburg-hire-me");
					edm1.setIsleaf(false); 
				}else{
					edm1.setIconCls("icon-hamburg-category");
					edm1.setState("closed");
				}
				listg.add(edm1);
			}
			edm.setChildren(listg);
			lists.add(edm);
		}else{
			 List<EasyuiDatagridMode> listg=new ArrayList<EasyuiDatagridMode>();
			 MailPabTableExample me=new MailPabTableExample();
			 me.createCriteria().andGroupCodeEqualTo(id)
			 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
			 List<MailPabTable> mt=mailpabmanager.selectByExample(me);
			 for(MailPabTable i:mt){
				 EasyuiDatagridMode edm1=new EasyuiDatagridMode();
					edm1.setId(i.getId().toString());
					edm1.setGroupName(i.getGroupName());
					edm1.setGroupCode(i.getGroupCode());
					edm1.setPerson(i.getPerson());
					edm1.setEmail(i.getEmail());
					edm1.setPhone(i.getPhone());
					if("1".equals(i.getPerson())){//为人员
						edm1.setIconCls("icon-hamburg-hire-me");
						edm1.setIsleaf(false); 
					}else{
						edm1.setIconCls("icon-hamburg-category");
						edm1.setState("closed");
					}
					listg.add(edm1);
			 }
			 lists=listg;
		}
		return JSON.toJSONString(lists);
	}
	
	
	/**
	 * 个人通讯录删除修改
	 * */
	@ResponseBody
	@RequestMapping("addOrUpdate")
	public String addOrUpdate(HttpSession session,String index,String ID,String Email,String Phone,String Type,String Name){
		Json J=new Json();
		try {
			MailPabTable recode=new MailPabTable();
			if("1".equals(index)){//添加
				recode.setEmail(Email);
				recode.setGroupName(Name);
				recode.setGroupCode(ID);
				recode.setLoginUser(session.getAttribute("MAIL_USER_ID").toString());
				recode.setPhone(Phone);
				recode.setPerson(Type);
				if(mailpabmanager.insert(recode)>0){
					J.setSuccess(true);
					J.setMsg("添加成功");
				}
			}else{//编辑
				recode=mailpabmanager.selectByPrimaryKey(new BigDecimal(ID));
				recode.setEmail(Email);
				recode.setGroupName(Name);
				recode.setPerson(Type);
				recode.setPhone(Phone);
				if(mailpabmanager.updateByPrimaryKey(recode)>0){
					J.setSuccess(true);
					J.setMsg("修改成功");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			J.setSuccess(true);
			J.setMsg("操作异常");
		}
		return JSON.toJSONString(J);
	}
	
	
	/**
	 * 删除
	 * */
	@ResponseBody
	@RequestMapping("remove")
	public String remove(HttpSession session,String ID,String type){
		Json J=new Json();
		try {
			if("0".equals(type)){//删除分组，删除组别下的所有用户
				if(!StringUtils.isBlank(ID)){
					if(mailpabmanager.deleteByPrimaryKey(new BigDecimal(ID))>0){
						//删除组下用户
						MailPabTableExample me=new MailPabTableExample();
						 me.createCriteria().andGroupCodeEqualTo(ID)
						 .andLoginUserEqualTo(session.getAttribute("MAIL_USER_ID").toString());
						 mailpabmanager.deleteByExample(me);
						 J.setSuccess(true);
						 J.setMsg("数据已删除");
					}else{
						J.setSuccess(false);
						J.setMsg("数据删除失败");
					}
				}
			}else{//删除用户
				if(!StringUtils.isBlank(ID)){
					if(mailpabmanager.deleteByPrimaryKey(new BigDecimal(ID))>0){
						J.setSuccess(true);
						J.setMsg("数据已删除");
					}else{
						J.setSuccess(false);
						J.setMsg("数据删除失败");
					}
				}
			}
		} catch (Exception e) {
			J.setSuccess(false);
			J.setMsg("数据删除失败");
		}
		return JSON.toJSONString(J);
	}
}
